<?php


namespace app\admin\model;


use think\Model;

class ReplyInfo extends Model
{
    protected $autoWriteTimestamp = true;

    /**
     * @param $replyId
     * @param $name
     * @param $info
     * @return bool
     */
    public static function add($replyId, $name, $info,$no,$duty): bool
    {
        $res = ReplyInfo::create([
            "reply_id" => $replyId, "name" => $name, "info" => $info,"create_staff_no"=>$no
        ,"staff_duty"=>$duty]);
        if (!$res) {
            return false;
        }
        return true;
    }

    /**
     * @param $id
     * @param $replyId
     * @param $name
     * @param $info
     * @return string
     */
    public static function up($id, $replyId, $name, $info): string
    {
        $res = ReplyInfo::where("id", $id)->find();
        if (!$res) {
            return "数据不存在";
        }
        $re["reply_id"] = $replyId;
        $re["name"] = $name;
        $re["info"] = $info;
        ReplyInfo::where("id",$id)->data($re)->update();
        return "";
    }

    /**
     * @param $id
     * @return bool|string
     * @throws \Exception
     */
    public static function del($id)
    {
        if(empty($id)) return "必须是数组";
        $res = ReplyInfo::where("id","in", $id)->select();
        if (!$res) return "数据不存在";
        $re = ReplyInfo::where("id","in", $id)->delete();
        if(!$re) return "删除失败";
        return "";
    }

    /**
     * @param $id
     * @return ReplyInfo|string
     */
    public static function det($id)
    {
        $res = ReplyInfo::alias("ri")
            ->field("ri.name,ri.info,ic.name as cate_name,ri.id,ri.reply_id")
            ->leftJoin("ic_reply ic", "ic.id = ri.reply_id")
            ->where("ri.id", $id)->find();
        return $res;
    }

    /**
     * @param $search
     * @param $replyId
     * @param $page
     * @param $limit
     * @return ReplyInfo[]|\think\Collection
     */
    public static function li($search,$createNo, $replyId, $page, $limit)
    {
        $resSql = ReplyInfo::alias("ri")
            ->field("ri.name,ri.info,ic.name as cate_name,ri.create_time,ri.id,is.staff_name,is.staff_no")
            ->leftJoin("ic_reply ic", "ic.id = ri.reply_id")
            ->leftJoin("ic_staff is", "is.staff_no = ri.create_staff_no")
            ->where("ri.staff_no","");
        $resSql = Duty::getQueryByDataPermId($resSql,"ri.create_staff_no");
       if ($search) {
           $resSql = $resSql->where("ri.info", $search);
       }
       if ($replyId > 0) {
           $resSql = $resSql->where("ri.reply_id", $replyId);
       }
        if ($createNo) {
            $resSql = $resSql->where("is.staff_no", $createNo);
        }
       $resC =$resSql->count();
       $res = $resSql->page($page,$limit)->select();
       return listDate($res,$resC);
    }


    /**
     * @param $info
     * @param $createNo
     * @param $replyId
     * @param $page
     * @param $limit
     * @return ReplyInfo[]|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function liMy($info,$createNo, $replyId,$page,$limit)
    {
        $resSql = ReplyInfo::alias("ri")
            ->field("ri.name,ri.info,ic.name as cate_name,ri.create_time,ri.id,is.staff_name,is.staff_no")
            ->leftJoin("ic_reply ic", "ic.id = ri.reply_id")
            ->leftJoin("ic_staff is", "is.staff_no = ri.create_staff_no")
            ->where("ri.staff_no","<>","");
        $resSql = Duty::getQueryByDataPermId($resSql,"ri.create_staff_no");
        if ($info) {
            $resSql = $resSql->where("ri.name", $info);
        }
        if ($createNo) {
            $resSql = $resSql->where("is.staff_no", $createNo);
        }
        if ($replyId > 0) {
            $resSql = $resSql->where("ri.reply_id", $replyId);
        }
        $resC =$resSql->count();
        $res = $resSql->page($page,$limit)->select();
        return listDate($res,$resC);
    }
}